<template>
  <basic-container>
    <avue-crud
      ref="crud"
      :page.sync="page"
      :data="tableData"
      :table-loading="tableLoading"
      :option="tableOption"
      @on-load="getList"
      @search-change="searchChange"
      @search-reset="searchReset"
    >
      <template slot="menuLeft">
        <el-button
          style="margin-right: 20px"
          type="primary"
          size="small"
          icon="el-icon-plus"
          @click="add('')"
        >
          新增区域
        </el-button>
      </template>
      <template
        slot="menu"
        slot-scope="scope"
      >
        <el-button
          type="text"
          @click="add(scope.row.id)"
        >
          编辑
        </el-button>
        <el-button
          type="text"
          @click="showCatList(scope.row.id)"
        >
          猫咪列表
        </el-button>
      </template>
    </avue-crud>
    <addAreaDialog
      :id="id"
      v-model="addVisible"
      @onSuccess="getList"
    ></addAreaDialog>
    <catListDialog
      :id="id"
      v-model="catVisible"
    ></catListDialog>
  </basic-container>
</template>
<script>
import crudMixins from '@/mixins/crud.js';
import { tableOption } from '@/const/crud/pet/adoptionArea.js';
import addAreaDialog from './components/addAreaDialog.vue';
import catListDialog from './components/catListDialog.vue';
import { fetchAdoptAreaList } from '@/api/core/adopt';
export default {
  components: {
    addAreaDialog,
    catListDialog,
  },
  mixins: [crudMixins],
  data() {
    return {
      tableOption,
      addVisible: false,
      catVisible: false,
      id: '',
    };
  },
  methods: {
    getList(page, refresh) {
      if (refresh) {
        this.page.current = 1;
        this.page.currentPage = 1;
      }
      this.tableLoading = true;
      this.tableLoading = false;
      fetchAdoptAreaList(
        Object.assign(
          {
            current: this.page.currentPage,
            size: this.page.pageSize,
          },
          this.searchForm
        )
      )
        .then((res) => {
          this.tableData = res.data.data.records;
          this.page.total = res.data.data.total;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    add(id) {
      this.id = id;
      this.addVisible = true;
    },
    showCatList(id) {
      this.id = id;
      this.catVisible = true;
    },
  },
};
</script>
